package com.heima.wemedia.config;

import com.heima.common.constants.message.PublishArticleConstants;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 配置延迟交换机 队列 和 绑定关系
 * 1. web页面 控制台
 * 2. @Configuration + @Bean 配置 绑定关系
 * 3. @RabbitListener (binding = )
 * @Author XHui
 * @Since 2024/2/15 17:56
 * @Version 1.0
 */
@Configuration
public class DeclarePubArticleRabbitConfig {

    /**
     * 声明 一个 direct类型的延迟交换机
     *
     * @return 直接交换
     */
    @Bean
    public DirectExchange declareDeclayDirectExchange() {
        return ExchangeBuilder
                .directExchange(PublishArticleConstants.DELAY_DIRECT_EXCHANGE)
                .delayed() // 设置延迟交换机
                .durable(true)
                .build();
    }

    @Bean
    public Queue declareDelayedQueue() {
        return QueueBuilder
                .durable(PublishArticleConstants.PUBLISH_ARTICLE_QUEUE) // 设置队列，持久化到硬盘将队列中的消息
                .build();
    }

    @Bean
    public Binding declareDelayedBinding() {
        return BindingBuilder
                .bind(declareDelayedQueue()) // 绑定队列
                .to(declareDeclayDirectExchange()) // 绑定交换机
                .with(PublishArticleConstants.PUBLISH_ARTICLE_ROUTE_KEY) // 路由键
                ;
    }
}
